package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.JueSeDao;
import net.mysoft.budgetsoft.system.dao.YongHuDao;
import net.mysoft.budgetsoft.system.dao.YongHuJSDao;
import net.mysoft.budgetsoft.system.domain.TGongNeng;
import net.mysoft.budgetsoft.system.domain.TGongNengJS;
import net.mysoft.budgetsoft.system.domain.TJueSe;
import net.mysoft.budgetsoft.system.domain.TYongHu;
import net.mysoft.budgetsoft.system.domain.TYongHuJS;
import net.mysoft.budgetsoft.system.pagemodel.DictionaryDetail;
import net.mysoft.budgetsoft.system.pagemodel.YongHu;
import net.mysoft.budgetsoft.system.service.YongHuService;
import net.mysoft.framework.cache.Cache;
import net.mysoft.framework.cache.CacheManager;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2013-02-06 11:02:08
 */
@Service("yongHuService")
public class YongHuServiceImpl implements YongHuService {
	@Resource
	private YongHuDao yongHuDao;
	@Resource
	private JueSeDao jueSeDao;
	@Resource
	private YongHuJSDao yongHuJSDao;

	public YongHuDao getYongHuDao() {
		return yongHuDao;
	}

	public void setYongHuDao(YongHuDao yongHuDao) {
		this.yongHuDao = yongHuDao;
	}

	public JueSeDao getJueSeDao() {
		return jueSeDao;
	}

	public void setJueSeDao(JueSeDao jueSeDao) {
		this.jueSeDao = jueSeDao;
	}

	public YongHuJSDao getYongHuJSDao() {
		return yongHuJSDao;
	}

	public void setYongHuJSDao(YongHuJSDao yongHuJSDao) {
		this.yongHuJSDao = yongHuJSDao;
	}

	@Override
	public YongHu login(YongHu yongHu) {
		TYongHu t = (TYongHu) yongHuDao.get("from TYongHu t where t.dengLuM=? and t.miMa=? and t.zhuangTai=11", new Object[] { yongHu.getDengLuM(), yongHu.getMiMa() });
		List<TGongNeng> fList = yongHuDao.find("from TGongNeng t where t.zhuangTai=11");
		if (t != null) {
			StringBuffer jueSeBHSB = new StringBuffer();
			StringBuffer jueSeMCSB = new StringBuffer();
			StringBuffer authIdsSB = new StringBuffer();

			Set<TGongNeng> gongNengSet = new HashSet();
			StringBuffer sb = new StringBuffer();
			sb.append("'" + t.getBianHao() + "'");
			Set<TYongHuJS> tYongHuJSSet = t.gettYongHuJSSet();
			if (tYongHuJSSet.size() > 0) {
				sb.append(",");
			}
			boolean b2 = false;
			for (TYongHuJS ur : tYongHuJSSet) {
				if (b2) {
					jueSeBHSB.append(",");
					jueSeMCSB.append(",");
					sb.append(",");
				}
				b2 = true;
				jueSeBHSB.append(ur.gettJueSe().getBianHao());
				jueSeMCSB.append(ur.gettJueSe().getJueSeMC());
				sb.append("'" + ur.gettJueSe().getBianHao() + "'");
			}

			List<TGongNengJS> aList = yongHuDao.find("from TGongNengJS t where t.jueSeBH in (" + sb.toString() + ")");
			for (TGongNengJS a : aList) {
				for (TGongNeng g : fList) {
					if (g.getBianHao().equals(a.getGongNengBH())) {
						loadResource(gongNengSet, g);
					}
				}
			}
			boolean b3 = false;
			for (TGongNeng tg : gongNengSet) {
				if (b3) {
					authIdsSB.append(",");
				}
				authIdsSB.append(tg.getBianHao());
				b3 = true;
			}
			BeanUtils.copyProperties(t, yongHu);
			yongHu.setAuthIds(authIdsSB.toString());
			yongHu.setJueSeBH(jueSeBHSB.toString());
			return yongHu;
		} else {
			return null;
		}
	}

	/**
	 * 递归添加资源
	 * 
	 * @param set
	 * @param tf
	 */
	private void loadResource(Set<TGongNeng> set, TGongNeng tf) {
		set.add(tf);
		if (tf.getPtGongNeng() != null) {
			loadResource(set, tf.getPtGongNeng());
		}
	}

	@Override
	public void add(YongHu yongHu) {
		TYongHu t = new TYongHu();
		BeanUtils.copyProperties(yongHu, t);
		t.setBianHao(UUID.randomUUID().toString());
		yongHuDao.save(t);
		this.saveYongHuJS(yongHu, t);
	}

	/**
	 * 保存用户和角色的关系
	 * 
	 * @param user
	 * @param u
	 */
	private void saveYongHuJS(YongHu yongHu, TYongHu u) {
		List list = yongHuJSDao.find("from TYongHuJS t where t.tYongHu = ?", new Object[] { u });
		yongHuJSDao.delete(list);
		if (yongHu.getJueSeBH() != null && !"".equals(yongHu.getJueSeBH())) {
			for (String id : yongHu.getJueSeBH().split(",")) {
				TYongHuJS tyongHuJS = new TYongHuJS();
				tyongHuJS.setBianHao(UUID.randomUUID().toString());
				TJueSe tJueSe = new TJueSe();
				tJueSe.setBianHao(id.trim());
				tyongHuJS.settJueSe(tJueSe);
				tyongHuJS.settYongHu(u);
				yongHuJSDao.save(tyongHuJS);
			}
		}
	}

	@Override
	public void edit(YongHu yongHu) {
		TYongHu t = new TYongHu();
		BeanUtils.copyProperties(yongHu, t);
		yongHuDao.update(t);
		this.saveYongHuJS(yongHu, t);
	}

	@Override
	public void editUserInfo(YongHu yongHu) {
		TYongHu t = (TYongHu) yongHuDao.load(TYongHu.class, yongHu.getBianHao());

		if (yongHu.getXingMing() != null && !"".equals(yongHu.getXingMing())) {
			t.setXingMing(yongHu.getXingMing());
		}
		if (yongHu.getMiMa() != null && !"".equals(yongHu.getMiMa())) {
			t.setMiMa(yongHu.getMiMa());
		}
		yongHuDao.update(t);

	}

	@Override
	public void delete(YongHu yongHu) {
		for (String id : yongHu.getIds().split(",")) {
			Object t = yongHuDao.get(TYongHu.class, id);
			if (t != null) {
				yongHuDao.delete(t);
			}
		}
	}

	@Override
	public DataGrid datagrid(YongHu yongHu) {
		DataGrid dg = new DataGrid();
		String hql = "from TYongHu t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(yongHu, hql, params);
		hql = addOrder(yongHu, hql);
		String totalHql = "select count(*) " + hql;
		List<TYongHu> toList = yongHuDao.find(hql, params, yongHu.getPage(), yongHu.getRows());
		List<YongHu> oList = new ArrayList<YongHu>();
		oList = changeModel(toList);
		dg.setTotal(yongHuDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}

	private String addWhere(YongHu yongHu, String hql, List<Object> params) {
		if (yongHu.getDengLuM() != null && !"".equals(yongHu.getDengLuM())) {
			hql += "and t.dengLuM like ? ";
			params.add("%" + yongHu.getDengLuM() + "%");
			System.out.println("user.getZhuangTai()=======================" + yongHu.getZhuangTai());
		}
		if (yongHu.getXingMing() != null && !"".equals(yongHu.getXingMing())) {
			hql += "and t.xingMing like ?";
			params.add("%" + yongHu.getXingMing() + "%");
		}
		if (yongHu.getZhuangTai() != null && !"".equals(yongHu.getZhuangTai())) {
			hql += "and t.zhuangTai = ?";
			params.add(yongHu.getZhuangTai());
			System.out.println("/************************" + yongHu.getZhuangTai());
		}
		if (yongHu.getUserCodes() != null && !"".equals(yongHu.getUserCodes())) {
			hql += " and t.bianHao in(";
			String[] str = yongHu.getUserCodes().split(",");
			for (int i = 0; i < str.length; i++) {
				if (i != 0) {
					hql += ",";
				}
				hql += ("'" + str[i] + "'");
			}
			hql += ")";
		}
		return hql;
	}

	private String addOrder(YongHu yongHu, String hql) {
		if (yongHu.getSort() != null) {
			hql += " order by " + yongHu.getSort() + " " + yongHu.getOrder();
		}
		return hql;
	}

	public List<YongHu> changeModel(List<TYongHu> tYongHu) {
		Cache cac = CacheManager.getContent("ZhuangTai");
		List<DictionaryDetail> ddList = new ArrayList<DictionaryDetail>();
		if (cac != null) {
			ddList = (List<DictionaryDetail>) cac.getValue();
		}

		List<YongHu> list = new ArrayList<YongHu>();
		for (TYongHu to : tYongHu) {
			YongHu o = new YongHu();
			BeanUtils.copyProperties(to, o);
			for (DictionaryDetail dd : ddList) {
				if (to.getZhuangTai() != null) {
					if (dd.getDetailID().equals(to.getZhuangTai().toString())) {
						o.setZhuangTaiStr(dd.getDetailName());
						break;
					}
				}
			}
			// 用户角色
			String jueSeBH = "";
			String jueSeMC = "";
			Set<TYongHuJS> yongHuJSSet = to.gettYongHuJSSet();
			for (TYongHuJS ur : yongHuJSSet) {
				jueSeBH += ur.gettJueSe().getBianHao() + ",";
				jueSeMC += ur.gettJueSe().getJueSeMC() + ",";
			}
			if (!"".equals(jueSeBH)) {
				jueSeBH = jueSeBH.substring(0, jueSeBH.length() - 1);
			}
			if (!"".equals(jueSeMC)) {
				jueSeMC = jueSeMC.substring(0, jueSeMC.length() - 1);
			}
			o.setJueSeBH(jueSeBH);
			o.setJueSeMC(jueSeMC);

			list.add(o);
		}
		return list;

		/*
		 * for (TYongHu to : tYongHu) { YongHu o = new YongHu(); BeanUtils.copyProperties(to, o); list.add(o); } return list;
		 */
	}

	@Override
	public String download(YongHu yongHu, String path) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public YongHu get(YongHu yongHu) {
		TYongHu to = (TYongHu) yongHuDao.get(TYongHu.class, yongHu.getBianHao());
		YongHu o = changeModel(to);
		return o;
	}

	@Override
	public YongHu get(String bianHao) {
		TYongHu to = (TYongHu) yongHuDao.get(TYongHu.class, bianHao);
		YongHu o = changeModel(to);
		return o;
	}

	public YongHu changeModel(TYongHu to) {
		YongHu o = new YongHu();
		BeanUtils.copyProperties(to, o);
		return o;
	}

	@Override
	public boolean check(YongHu yongHu) {
		String dengLuM = yongHu.getDengLuM();
		List<TYongHu> list = null;
		if (dengLuM != null && !"".equals(dengLuM)) {
			list = yongHuDao.find("from TYongHu t where t.dengLuM = ?", new String[] { dengLuM });
		}
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean checkEdit(YongHu yongHu) {
		/*String dengLuM = yongHu.getDengLuM();
		String tDengLuM = ((TYongHu) yongHuDao.get(TYongHu.class, yongHu.getBianHao())).getDengLuM();
		List<TQiYeJBXX> list = null;
		if(dengLuM.equals(tDengLuM)) {
			return false;
		} else {
			list = yongHuDao.find("from TYongHu t where t.dengLuM = ?", new String[] { dengLuM });
		}
		if (list != null && list.size() > 0) {
			return true;
		}*/
		return false;
	}

	@Override
	public List find(YongHu yongHu) {
		String hql = "from TYongHu t where 1=1";
		List paramList = new ArrayList();
		hql = addWhere(yongHu, hql, paramList);
		List<TYongHu> toList = yongHuDao.find(hql, paramList);
		List<YongHu> oList = new ArrayList<YongHu>();
		oList = changeModel(toList);
		return oList;
	}

}
